const express = require('express');
const router = express.Router();
const authController = require('../controllers/auth');
const jwt = require('jsonwebtoken');
const { jwtConfig } = require('../config/config');

// 注册路由
router.post('/register', authController.register);

// 登录路由
router.post('/login', authController.login);

// 生成token路由（用于调试）
router.post('/generate-token', (req, res) => {
    try {
        const { userId, phone } = req.body;
        
        if (!userId || !phone) {
            return res.status(400).json({
                success: false,
                message: '用户ID和手机号不能为空'
            });
        }
        
        // 生成JWT令牌
        const token = jwt.sign(
            { userId, phone },
            jwtConfig.secret,
            { expiresIn: jwtConfig.expiresIn }
        );
        
        // 解码token查看内容
        const decoded = jwt.decode(token);
        
        res.json({
            success: true,
            message: 'Token生成成功',
            token,
            decoded
        });
    } catch (error) {
        console.error('生成token失败:', error);
        res.status(500).json({
            success: false,
            message: '生成token失败'
        });
    }
});

module.exports = router; 